﻿using System;
using System.IO;

using E.BLL;
using E.Model;
using E.Web.UI.Page;

using X.Common;

namespace E.Web.Teacher.courseware
{
    public partial class H : TeacherBasePage
    {
        protected override void ShowPage()
        {
            int id = XRequest.GetInt("id", -1);
            int rst = 0;
            string msg, href;

            if (id != -1)
            {
                CourseBLL bll = new CourseBLL();
                CourseWareModel model = bll.GetCourseWareInfoById(id);
                string imgurl = GetSubValue(model.ImgUrl);
                string soundurl = GetSubValue(model.SoundUrl);
                string videourl = GetSubValue(model.VideoUrl);
                int err = 0;
                string serverPath = System.Web.HttpContext.Current.Server.MapPath("/");

                if (!imgurl.Equals(""))
                {
                    try
                    {
                        File.Delete(serverPath + imgurl);
                    }
                    catch (IOException ioEx)
                    {
                        err++;
                    }
                }

                if (!soundurl.Equals(""))
                {
                    try
                    {
                        File.Delete(serverPath + soundurl);
                    }
                    catch (IOException ioEx)
                    {
                        err++;
                    }
                }

                if (!videourl.Equals(""))
                {
                    try
                    {
                        File.Delete(serverPath + videourl);
                    }
                    catch (IOException ioEx)
                    {
                        err++;
                    }
                }

                if (err == 0)
                {
                    rst = bll.DelCourseWare(id);
                    if (rst > 0)
                    {
                        bll.UpdateSerialNumber(model.SerialNumber, model.CourseID);
                        msg = "操作成功";
                        href = "window.location.replace('/courseware/List.aspx');";
                    }
                    else
                    {
                        msg = "操作失败";
                        href = "history.back();";
                    }
                }
                else
                {
                    msg = "文件删除错误";
                    href = "history.back();";
                }
            }
            else
            {
                msg = "参数错误";
                href = "history.back();";
            }
            ExecuteJS("alert('" + msg + "'); " + href);
        }

        private string GetSubValue(string v)
        {
            if (!v.Equals(""))
            {
                if (v.IndexOf("//") != -1)
                {
                    v = v.Substring(v.IndexOf("//") + 2);
                    v = v.Substring(v.IndexOf("/"));
                }
            }
            return v;
        }

        protected void Page_Load(object sender, EventArgs e)
        {


        }
    }
}